﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OPMS_V1.DOL;
using OPMS_V1.BOL;
using System.Data;

namespace OPMS_V1.Public.ManageAds
{
    public partial class PropertyList : System.Web.UI.Page
    {
        //Initializing the necessary Classes needed
        S_PDataContext dc = new S_PDataContext();
        ListProperties list = new ListProperties();
        ManageProperties manage = new ManageProperties();
        User user = new User();
        DataSet ds = new DataSet();

        /// <summary>
        /// Autoimplemented Property for RecordsExists or not
        /// </summary>
        public string recordsExists
        { get; set; }

        /// <summary>
        /// This function is used for PageLoad event.
        /// Here i have used !Page.IsPostBack because,
        /// i do not want the grid view to be binded with the last data which was present
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string LoginID = Session["user"].ToString();

            //If no session exists then redirect to SelfService.aspx
            if (LoginID == null || LoginID == "")
            {
                Session["statusPage"] = "Ad";
                Response.Redirect("~\\Public\\SelfService.aspx?Mode=" + Session["statusPage"].ToString());
            }

            if (!Page.IsPostBack)
            {
                //Collecting all the information into one datatable

                //This section is used for displaying the "completed" property ads to the Grid View
                #region This part will result in all the completed property Ads of the particular User

                //Binding the grid view with the records present in datatable
                grdPropertyList.DataSource = manage.GetCompletedPropertyList();
                grdPropertyList.DataBind();

                #endregion This part will show all the completed property ads of the particular User

                //This section is for displaying the incompleted property ads to the Grid View
                #region This part will show all the "incompleted" property ads of the particular User

                //Binding the grid view with the records present in datatable
                grdIncomplete.DataSource = manage.GetIncompletedProperties();
                grdIncomplete.DataBind();

                #endregion This part will show all the "incompleted" property ads of the particular User


                //This section is used for displaying the User information on to the Page of Personal details
                #region Display User Personal Information
                lblRegisterError.Visible = false;
                //regexReqPassword.Enabled = false;
                lblRequired.Visible = false;
                Boolean successful = false;

                try
                {
                    if (Session["user"].ToString() != "")
                    {
                        //Displaying the Login ID on to the Form
                        LoginID = HttpContext.Current.Session["user"].ToString();
                        lblRegAccount.Text = HttpContext.Current.Session["user"].ToString();
                        successful = true;
                    }
                    else
                    {
                        //If no Session exists then redirect to SelfService
                        Session["statusPage"] = "Ad";
                        Response.Redirect("~\\Public\\SelfService.aspx?Mode=" + Session["statusPage"].ToString());
                    }

                    //If the user is successfully login and had a session
                    if (successful == true)
                    {
                        //Get the User Personal details
                        ds = user.Get_UserDetails(LoginID);
                        try
                        {
                            //Checking for the Login ID exists or not
                            recordsExists = ds.Tables[0].Rows[0][0].ToString();
                        }
                        catch (Exception)
                        {
                            recordsExists = null;
                        }
                        try
                        {
                            //If the LoginID exists then get all the Personal Information to the TextBoxes
                            if (recordsExists != null)
                            {
                                Session["recordExists"] = "true";
                                //Get the Login ID
                                lblRegAccount.Text = ds.Tables[0].Rows[0][0].ToString();
                                //Get the First Name
                                txtFirstName.Text = ds.Tables[0].Rows[0][1].ToString();
                                //Get the Last Name
                                txtLastName.Text = ds.Tables[0].Rows[0][2].ToString();
                                //Get the Sex
                                string sex = ds.Tables[0].Rows[0][3].ToString();
                                if (sex == "Male")
                                {
                                    rdbMale.Checked = true;
                                }
                                else { rdbFemale.Checked = true; }

                                //Get the Phone Number
                                txtPhone.Text = ds.Tables[0].Rows[0][4].ToString();
                                //Get the StreetNum
                                txtStreetNum.Text = ds.Tables[0].Rows[0][5].ToString();
                                //Get the Street Name
                                txtStreetName.Text = ds.Tables[0].Rows[0][6].ToString();
                                //Get the PostCode
                                txtPostCode.Text = ds.Tables[0].Rows[0][7].ToString();
                                //Get the Area Name
                                txtArea.Text = ds.Tables[0].Rows[0][8].ToString();
                                //Get the State
                                string state = ds.Tables[0].Rows[0][9].ToString();
                                ddlState.SelectedValue = state;
                            }
                        }
                        catch (Exception)
                        { }
                    }
                }
                catch (Exception)
                {
                    //If some errors occured then redirect to SelfService
                    Session["statusPage"] = "Ad";
                    Response.Redirect("~\\Public\\SelfService.aspx?Mode=" + Session["statusPage"].ToString());
                }
                #endregion Display User Personal Information
            }
        }

        /// <summary>
        /// This function is used for Page index change of Grid View to see multiple records
        /// It is executed when the User requests for Page Index change (Complete List)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdPropertyList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //Getting the new Page for the Grid View
            grdPropertyList.PageIndex = e.NewPageIndex;
            //Get the data from GetDataSource
            grdPropertyList.DataSource = manage.GetDataSource();
            //and bind it to the Grid View
            grdPropertyList.DataBind();
        }

        /// <summary>
        /// This function is used for Page index change of Grid View to see multiple records
        /// It is executed when the User requests for Page Index change (Incomplete List)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdIncomplete_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //Getting the new Page for the Grid View
            grdIncomplete.PageIndex = e.NewPageIndex;
            //Get the data from GetDataSource
            grdIncomplete.DataSource = manage.GetIncompleteDataSource();
            //and bind it to the Grid View
            grdIncomplete.DataBind();
        }


        /// <summary>
        /// This function is used for Updating the User Details
        /// It checks for the existance of Normal User Details onto the System
        /// If the User Details exists then Update otherwise insert the records
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region Update User personal Information to the Database
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string LoginID = Session["user"].ToString();
            Boolean condition = false;
            //Get the Login ID
            string loginID = lblRegAccount.Text;
            //Get the First Name
            string fName = txtFirstName.Text;
            //Get the Last Name
            string lName = txtLastName.Text;
            //Get the Sex
            string sex = "";
            if (rdbFemale.Checked == true)
            {
                sex = "Female";
            }
            else if (rdbMale.Checked == true)
            {
                sex = "Male";
            }

            //Get the Phone Number
            string phone = txtPhone.Text;
            //Get the Street Num
            string streetnum = txtStreetNum.Text;
            //Get the Street Name
            string streetName = txtStreetName.Text;
            //Get the Post Code
            string postCode = txtPostCode.Text;
            //Get the Area Name
            string area = txtArea.Text;
            //Get the State
            string state = ddlState.SelectedValue;

            //Checking for Validity
            if (fName.Trim() == "" || lName.Trim() == "" || phone.Trim() == "" || streetnum.Trim() == "" || streetName.Trim() == "" || postCode.Trim() == "" || area.Trim() == "")
            {
                lblRequired.Visible = true;
                pnlRegistration.Visible = true;
                condition = false;
            }
            //If the required details are present then proceed
            else
            {
                //If the user accepts terms and condition
                if (chkTerms.Checked == true)
                {
                    //Get the Personal Information
                    ds = user.Get_UserDetails(LoginID);
                    try
                    {
                        //Check for Existance of the User
                        recordsExists = ds.Tables[0].Rows[0][0].ToString();
                    }
                    catch (Exception)
                    {
                        recordsExists = null;
                    }
                    try
                    {
                        //If the user records are present then
                        if (recordsExists != null)
                        {
                            try
                            {
                                //Update the Details, if any changes made
                                dc.Update_User_Details(@fName, @lName, @sex, @phone, @streetnum, @streetName, @postCode, @area, @state, @loginID);
                                lblRequired.Visible = true;
                                lblRequired.Text = "Your records has been updated.";
                                pnlRegistration.Visible = true;
                            }
                            catch (Exception)
                            {
                                lblRequired.Visible = true;
                                lblRequired.Text = "Your records has not been updated.";
                                pnlRegistration.Visible = true;
                                condition = false;
                            }
                        }
                        else
                        {
                            //Insert the records if no recods are present
                            try
                            {
                                dc.Insert_UserDetails(@fName, @lName, @sex, @phone, @streetnum, @streetName, @postCode, @area, @state, @loginID);
                                lblRequired.Visible = true;
                                lblRequired.Text = "Your records has been updated.";
                                pnlRegistration.Visible = true;
                            }
                            catch (Exception)
                            {
                                lblRequired.Visible = true;
                                lblRequired.Text = "Your records has not been updated.";
                                pnlRegistration.Visible = true;
                                condition = false;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        lblRequired.Visible = true;
                        lblRequired.Text = "Your records has not been updated.";
                        pnlRegistration.Visible = true;
                        condition = false;
                    }
                }
                else
                {
                    //Not accepted the terms and conditions
                    lblRequired.Visible = true;
                    lblRequired.Text = "Please accept our terms and conditions..";
                    pnlRegistration.Visible = true;
                    condition = false;
                }
            }
        }
        #endregion Update User personal Information to the Database


        /// <summary>
        /// This function is used for Placing a new Ad
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNewAd_Click(object sender, EventArgs e)
        {
            //Redirect the user to the options Page to place an Ad
            Session["statusPage"] = "Ad";
            Response.Redirect("~\\Public\\SelfService.aspx?Mode=" + Session["statusPage"].ToString());
        }


        /// <summary>
        /// This function is used for Edit Property for Completed List of Properties
        /// Its main purpose is to create sessions and then redirect the user to The Editing Mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdPropertyList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Boolean successful = false;
            DataSet ds = new DataSet();
            grdPropertyList.EditIndex = e.NewEditIndex;
            //Getting the list of property ID and Property Type
            string propID = grdPropertyList.Rows[grdPropertyList.EditIndex].Cells[3].Text;
            string propType = grdPropertyList.Rows[grdPropertyList.EditIndex].Cells[4].Text;

            try
            {
                //Managing the Property Type (Getting the comoplete details of the property)
                manage.propertyEdit(propID, propType);
                successful = true;
            }
            catch (Exception)
            {
                successful = false;
            }

            if (successful == true)
            {
                Session["EditOption"] = "true";
                string AdType = Session["propertyAdType"].ToString();
                Response.Redirect("~//Public//StartAd.aspx");
            }
        }


        /// <summary>
        /// This function is used for Edit Property for Completed List of Properties
        /// Its main purpose is to create sessions and then redirect the user to The Editing Mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdIncomplete_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Boolean successful = false;
            DataSet ds = new DataSet();
            grdPropertyList.EditIndex = e.NewEditIndex;
            //Getting the list of property ID and Property Type
            string propID = grdIncomplete.Rows[grdPropertyList.EditIndex].Cells[3].Text;
            string propType = grdIncomplete.Rows[grdPropertyList.EditIndex].Cells[4].Text;

            try
            {
                //Managing the Property Type (Getting the comoplete details of the property)
                manage.propertyEdit(propID, propType);
                successful = true;
            }
            catch (Exception)
            {
                successful = false;
            }

            if (successful == true)
            {
                Session["EditOption"] = "true";
                string AdType = Session["propertyAdType"].ToString();
                Response.Redirect("~//Public//StartAd.aspx");
            }
        }

        protected void grdPropertyList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}

